<script lang="ts" setup>
import { useAuthStore } from 'stores/authStore'
import { storeToRefs } from 'pinia'

export interface Note {
  id: number
  cover: string
  title: string
  description?: string
  authorAvatar: string
  authorNickname: string
  liked: boolean
  collected: boolean
}

const props = defineProps<{
  note: Note
}>()

const { certified, showLoginDialog } = storeToRefs(useAuthStore())
const emit = defineEmits(['update:like', 'update:open', 'update:selectedNote'])

function updateLike() {
  if (!certified.value && (showLoginDialog.value = true)) return
  const change = !props.note.liked
  emit('update:like', change)
  // TODO: 发送http请求更改点赞状态
}

function selectNote() {
  emit('update:open', {
    showing: true,
    selectedNote: props.note,
  })
}
</script>

<template>
  <div class="note">
    <div class="cover" @click="selectNote">
      <q-img
        loading="eager"
        style="border-radius: 8px 8px 0 0"
        :src="note.cover"
      />
      <div class="title">
        {{ note.title }}
      </div>
    </div>

    <div class="footer">
      <div class="wrapper">
        <div class="avatar">
          <q-avatar size="sm" style="margin-right: 5px">
            <img :src="note.authorAvatar" />
          </q-avatar>
          {{ note.authorNickname }}
        </div>
        <!-- TODO: 点赞数加1需要判断 -->
        <q-btn
          size="sm"
          icon="favorite"
          unelevated
          rounded
          color="orange-4"
          :style="{ color: (note.liked ? '#ff5e57' : 'white') + ' !important' }"
          @click="updateLike"
        />
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.note {
  width: 215px;
  box-shadow: rgba(149, 157, 165, 0.2) 0px 8px 24px;
  margin: 0.6em;
  border-radius: 8px;
  .cover {
    cursor: pointer;
    .title {
      padding: 12px 12px 0px 12px;
      word-break: break-all;
      font-weight: bold;
    }
    transition: all 0.3s linear;
    &:hover {
      filter: brightness(80%);
    }
  }

  .footer {
    width: 100%;
    padding: 12px;

    .wrapper {
      display: flex;
      align-items: center;
      font-size: 12px;
      font-weight: bold;
      color: $stress-color;
      justify-content: space-between;

      .avatar {
        cursor: pointer;
        max-width: 110px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
}
</style>
